package com.ycy;

import com.ycy.servlet.ForServletContainerYcyServiceServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.util.Set;


/**
 * ServiceLoader<Search> load = ServiceLoader.load(Search.class);
 */
public class CustomServletContainerInitializer implements ServletContainerInitializer {

  Logger logger = LoggerFactory.getLogger(this.getClass());

  @Override
  public void onStartup(Set<Class<?>> c, ServletContext servletContext) throws ServletException {

    //动态添加servlet
    logger.warn("创建 serviceservlet {} 开始", ForServletContainerYcyServiceServlet.class.getSimpleName());
    ServletRegistration.Dynamic servlet = servletContext.addServlet(
            ForServletContainerYcyServiceServlet.class.getSimpleName(),
            ForServletContainerYcyServiceServlet.class);
    servlet.addMapping("/hello2");
    logger.warn("创建 serviceservlet {} 结束", ForServletContainerYcyServiceServlet.class.getSimpleName());

    //动态添加filter
//    FilterRegistration.Dynamic filter = servletContext.addFilter(
//            YcyFilter.class.getSimpleName(),
//            YcyFilter.class);
//    EnumSet<DispatcherType> dispatcherTypes = EnumSet.allOf(DispatcherType.class);
//    dispatcherTypes.add(DispatcherType.REQUEST);
//    dispatcherTypes.add(DispatcherType.FORWARD);
//    filter.addMappingForUrlPatterns(dispatcherTypes
//            , true
//            , "/*");


  }

}
